home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / dobbs / v16n09 / bob12.exe / BOB.H < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-18  |  9.4 KB  |  291 lines

  1. /* bob.h - bob definitions */
  2. /*
  3.     Copyright (c) 1991, by David Michael Betz
  4.     All rights reserved
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9.  
  10. /* limits */
  11. #define TKNSIZE        50    /* maximum token size */
  12. #define SMAX        500    /* runtime stack size */
  13. #define CMAX        32767    /* code buffer size */
  14.  
  15. /* useful definitions */
  16. #define TRUE        1
  17. #define FALSE        0
  18.  
  19. /* token definitions */
  20. #define T_NOTOKEN    -1
  21. #define T_EOF        0
  22.  
  23. /* non-character tokens */
  24. #define _TMIN        256
  25. #define T_STRING    256
  26. #define T_IDENTIFIER    257
  27. #define T_NUMBER    258
  28. #define T_CLASS        259
  29. #define T_STATIC    260
  30. #define T_IF        261
  31. #define T_ELSE        262
  32. #define T_WHILE        263
  33. #define T_RETURN    264
  34. #define T_FOR        265
  35. #define T_BREAK        266
  36. #define T_CONTINUE    267
  37. #define T_DO        268
  38. #define T_NEW        269
  39. #define T_NIL        270
  40. #define T_LE        271    /* '<=' */
  41. #define T_EQ        272    /* '==' */
  42. #define T_NE        273    /* '!=' */
  43. #define T_GE        274    /* '>=' */
  44. #define T_SHL        275    /* '<<' */
  45. #define T_SHR        276    /* '>>' */
  46. #define T_AND        277    /* '&&' */
  47. #define T_OR        278    /* '||' */
  48. #define T_INC        279    /* '++' */
  49. #define T_DEC        280    /* '--' */
  50. #define T_ADDEQ        281    /* '+=' */
  51. #define T_SUBEQ        282    /* '-=' */
  52. #define T_MULEQ        283    /* '*=' */
  53. #define T_DIVEQ        284    /* '/=' */
  54. #define T_REMEQ        285    /* '%=' */
  55. #define T_ANDEQ        286    /* '&=' */
  56. #define T_OREQ        287    /* '|=' */
  57. #define T_XOREQ        288    /* '^=' */
  58. #define T_SHLEQ        289    /* '<<=' */
  59. #define T_SHREQ        290    /* '>>=' */
  60. #define T_CC        291    /* '::' */
  61. #define T_MEMREF    292    /* '->' */
  62. #define _TMAX        292
  63.  
  64. /* stack manipulation macros */
  65. #define check(n)     { if (sp - (n) < stkbase) stackover(); }
  66. #define chktype(o,t)     { if (sp[o].v_type != t) badtype(o,t); }
  67. #define push(x,t,f)     (--sp, sp->v_type = (t), sp->v.f = (x))
  68. #define push_integer(x)     push(x,DT_INTEGER,v_integer)
  69. #define push_class(x)     push(x,DT_CLASS,v_class)
  70. #define push_object(x)     push(x,DT_OBJECT,v_object)
  71. #define push_bytecode(x) push(x,DT_BYTECODE,v_vector)
  72. #define push_var(x)     push(x,DT_VAR,v_var)
  73. #define push_nil()     (--sp, sp->v_type = DT_NIL)
  74.  
  75. /* macros to set values */
  76. #define set(s,x,t,f)        ((s)->v.f = (x), (s)->v_type = (t))
  77. #define set_integer(s,x)    set(s,x,DT_INTEGER,v_integer)
  78. #define set_class(s,x)        set(s,x,DT_CLASS,v_class)
  79. #define set_object(s,x)     set(s,x,DT_OBJECT,v_object)
  80. #define set_code(s,x)       set(s,x,DT_CODE,v_code)
  81. #define set_bytecode(s,x)   set(s,x,DT_BYTECODE,v_vector)
  82. #define set_dictionary(s,x) set(s,x,DT_DICTIONARY,v_dictionary)
  83. #define set_var(s,x)        set(s,x,DT_VAR,v_var)
  84. #define set_string(s,x)        set(s,x,DT_STRING,v_string)
  85. #define set_vector(s,x)        set(s,x,DT_VECTOR,v_vector)
  86. #define set_file(s,x)        set(s,x,DT_FILE,v_fp)
  87. #define set_nil(s)        ((s)->v_type = DT_NIL)
  88.  
  89. /* value field access macros */
  90. #define valtype(x)        ((x)->v_type)
  91. #define isnil(x)        ((x)->v_type == DT_NIL)
  92.  
  93. /* class field access macros */
  94. #define claddr(x)        ((x)->v.v_class)
  95. #define clgetname(x)        (&claddr(x)->cl_name)
  96. #define clgetbase(x)        (&claddr(x)->cl_base)
  97. #define clgetmembers(x)        (&claddr(x)->cl_members)
  98. #define clgetfunctions(x)    (&claddr(x)->cl_functions)
  99. #define clgetsize(x)        (claddr(x)->cl_size)
  100.  
  101. /* object field access macros */
  102. #define objaddr(x)        ((x)->v.v_object)
  103. #define objgetclass(x)        (&objaddr(x)->obj_class)
  104. #define objgetmember(x,i)    (&objaddr(x)->obj_members[i])
  105. #define objsetmember(x,i,v)    (objaddr(x)->obj_members[i] = (v))
  106.  
  107. /* vector field access macros */
  108. #define vecaddr(x)        ((x)->v.v_vector)
  109. #define vecgetsize(x)        (vecaddr(x)->vec_size)
  110. #define vecgetelement(x,i)    (&vecaddr(x)->vec_data[i])
  111. #define vecsetelement(x,i,v)    (vecaddr(x)->vec_data[i] = (v))
  112.  
  113. /* string field access macros */
  114. #define straddr(x)        ((x)->v.v_string)
  115. #define strgetsize(x)        (straddr(x)->str_size)
  116. #define strgetdata(x)        (straddr(x)->str_data)
  117.  
  118. /* dictionary field access macros */
  119. #define diaddr(x)        ((x)->v.v_dictionary)
  120. #define digetclass(x)        (&diaddr(x)->di_class)
  121. #define digetcontents(x)    (&diaddr(x)->di_contents)
  122.  
  123. /* dictionary entry field access macros */
  124. #define deaddr(x)        ((x)->v.v_var)
  125. #define degetdictionary(x)    (&deaddr(x)->de_dictionary)
  126. #define degetkey(x)        (&deaddr(x)->de_key)
  127. #define degetvalue(x)        (&deaddr(x)->de_value)
  128. #define degetnext(x)        (&deaddr(x)->de_next)
  129. #define degettype(x)        (deaddr(x)->de_type)
  130.  
  131. /* value descriptor structure */
  132. typedef struct value {
  133.   int v_type;                /* data type */
  134.   union {                /* value */
  135.     struct class *v_class;        /*   class (in heap) */
  136.     struct object *v_object;        /*   object (in heap) */
  137.     struct vector *v_vector;        /*   vector (in heap) */
  138.     struct string *v_string;        /*   string (in heap) */
  139.     struct dictionary *v_dictionary;    /*   dictionary (in heap) */
  140.     struct dict_entry *v_var;        /*   variable (in heap) */
  141.     int (*v_code)();            /*   code for built-in function */
  142.     long v_integer;            /*   integer */
  143.     FILE *v_fp;                /*   file pointer */
  144.     struct hdr *v_hdr;            /*   (used by garbage collector) */
  145.     struct value *v_chain;        /*   (used by garbage collector) */
  146.   } v;
  147. } VALUE;
  148.  
  149. typedef struct hdr {
  150.     char hdr_type;
  151.     char hdr_flags;
  152.     VALUE *hdr_chain;
  153. } HDR;
  154.  
  155. typedef struct class {
  156.     HDR cl_hdr;
  157.     VALUE cl_name;
  158.     VALUE cl_base;
  159.     VALUE cl_members;
  160.     VALUE cl_functions;
  161.     int cl_size;
  162. } CLASS;
  163.  
  164. typedef struct object {
  165.     HDR obj_hdr;
  166.     VALUE obj_class;
  167.     VALUE obj_members[1];
  168. } OBJECT;
  169.  
  170. typedef struct vector {
  171.     HDR vec_hdr;
  172.     int vec_size;
  173.     VALUE vec_data[1];
  174. } VECTOR;
  175.  
  176. typedef struct string {
  177.     HDR str_hdr;
  178.     int str_size;
  179.     char str_data[1];
  180. } STRING;
  181.  
  182. typedef struct dictionary {
  183.     HDR di_hdr;
  184.     VALUE di_class;
  185.     VALUE di_contents;
  186. } DICTIONARY;
  187.  
  188. /* dictionary entry structure */
  189. typedef struct dict_entry {
  190.     HDR de_hdr;
  191.     VALUE de_dictionary;    /* backpointer to dictionary */
  192.     VALUE de_key;        /* symbol name */
  193.     int de_type;        /* symbol type */
  194.     VALUE de_value;        /* symbol value */
  195.     VALUE de_next;        /* next entry */
  196. } DICT_ENTRY;
  197.  
  198. /* symbol types */
  199. #define ST_CLASS    1    /* class definition */
  200. #define ST_DATA        2    /* data member */
  201. #define ST_SDATA    3    /* static data member */
  202. #define ST_FUNCTION    4    /* function member */
  203. #define ST_SFUNCTION    5    /* static function member */
  204.  
  205. /* data types */
  206. #define _DTMIN        0
  207. #define DT_NIL        0
  208. #define DT_CLASS    1
  209. #define DT_OBJECT    2
  210. #define DT_VECTOR    3
  211. #define DT_INTEGER    4
  212. #define DT_STRING    5
  213. #define DT_BYTECODE    6
  214. #define DT_CODE        7
  215. #define DT_DICTIONARY    8
  216. #define DT_VAR        9
  217. #define DT_FILE        10
  218. #define _DTMAX        10
  219.  
  220. /* function argument structure */
  221. typedef struct argument {
  222.     char *arg_name;        /* argument name */
  223.     struct argument *arg_next;    /* next argument */
  224. } ARGUMENT;
  225.  
  226. /* literal structure */
  227. typedef struct literal {
  228.     VALUE lit_value;        /* literal value */
  229.     struct literal *lit_next;    /* next literal */
  230. } LITERAL;
  231.  
  232. /* opcodes */
  233. #define OP_BRT        0x01    /* branch on true */
  234. #define OP_BRF        0x02    /* branch on false */
  235. #define OP_BR        0x03    /* branch unconditionally */
  236. #define OP_NIL        0x04    /* load top of stack with nil */
  237. #define OP_PUSH        0x05    /* push nil onto stack */
  238. #define OP_NOT        0x06    /* logical negate top of stack */
  239. #define OP_NEG        0x07    /* negate top of stack */
  240. #define OP_ADD        0x08    /* add top two stack entries */
  241. #define OP_SUB        0x09    /* subtract top two stack entries */
  242. #define OP_MUL        0x0A    /* multiply top two stack entries */
  243. #define OP_DIV        0x0B    /* divide top two stack entries */
  244. #define OP_REM        0x0C    /* remainder of top two stack entries */
  245. #define OP_BAND        0x0D    /* bitwise and of top two stack entries */
  246. #define OP_BOR        0x0E    /* bitwise or of top two stack entries */
  247. #define OP_XOR        0x0F    /* bitwise xor of top two stack entries */
  248. #define OP_BNOT        0x10    /* bitwise not of top two stack entries */
  249. #define OP_SHL        0x11    /* shift left top two stack entries */
  250. #define OP_SHR        0x12    /* shift right top two stack entries */
  251. #define OP_LT        0x13    /* less than */
  252. #define OP_LE        0x14    /* less than or equal to */
  253. #define OP_EQ        0x15    /* equal to */
  254. #define OP_NE        0x16    /* not equal to */
  255. #define OP_GE        0x17    /* greater than or equal to */
  256. #define OP_GT        0x18    /* greater than */
  257. #define OP_INC        0x19    /* increment */
  258. #define OP_DEC        0x1A    /* decrement */
  259. #define OP_LIT        0x1B    /* load literal */
  260. #define OP_RETURN    0x1C    /* return from interpreter */
  261. #define OP_CALL        0x1D    /* call a function */
  262. #define OP_REF        0x1E    /* load a variable value */
  263. #define OP_SET        0x1F    /* set the value of a variable */
  264. #define OP_VREF        0x20    /* load a vector element */
  265. #define OP_VSET        0x21    /* set a vector element */
  266. #define OP_MREF        0x22    /* load a member variable value */
  267. #define OP_MSET        0x23    /* set a member variable */
  268. #define OP_AREF        0x24    /* load an argument value */
  269. #define OP_ASET        0x25    /* set an argument value */
  270. #define OP_TREF        0x26    /* load a temporary variable value */
  271. #define OP_TSET        0x27    /* set a temporary variable */
  272. #define OP_TSPACE    0x28    /* allocate temporary variable space */
  273. #define OP_SEND        0x29    /* send a message to an object */
  274. #define OP_DUP2        0x2A    /* duplicate top two elements on the stack */
  275. #define OP_NEW        0x2B    /* create a new class object */
  276.  
  277. /* external variables */
  278. extern VALUE *stkbase,*sp,*fp,*stktop;
  279. extern VALUE nil;
  280.  
  281. /* external routines */
  282. extern CLASS *newclass();
  283. extern OBJECT *newobject();
  284. extern VECTOR *newvector();
  285. extern STRING *newstring();
  286. extern STRING *makestring();
  287. extern DICTIONARY *newdictionary();
  288. extern DICT_ENTRY *findentry();
  289. extern DICT_ENTRY *addentry();
  290. extern char *getcstring();
  291.